//
//  NewFMViewController.swift
//  FM
//
//  Created by x on 2017/11/6.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

class NewFMViewController: UIViewController {

    var is_teacher:Int?
    
    var index:Int = 0
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }

    func setupUI() -> Void {
        
        view.backgroundColor = UIColor.white
        
        view.addSubview(fmTableView)
        
        //加载数据
        loadFM(offset: 0)
        
    }
    
    /// 加载最多
    func loadMoreFM() -> Void {
        index += 1
        let num = index * 15
        loadFM(offset: num)
    }
    
    /// 加载最FM数据
    ///
    /// - Parameter offset: <#offset description#>
    func loadFM(offset:Int) -> Void {
        NetWorTools.shareNetWorkTools().loadNewFMorPsychology(is_teacher: is_teacher!, offset: offset) { (data, error) in
            if error == nil {
                self.newFMlist += data!
                self.fmTableView.reloadData()
            }else {
                print("loadFM======",error!)
            }
        }
    }
    
    private lazy var fmTableView:UITableView = {
        let tableView = UITableView()
        tableView.frame = self.view.bounds
        tableView.dataSource = self
        tableView.delegate = self
        
        tableView.register(CategoryTableViewCell.self, forCellReuseIdentifier: newFMCellIdentifier)
        tableView.mj_footer = MJRefreshBackNormalFooter(refreshingTarget: self, refreshingAction: #selector(loadMoreFM))
        
        return tableView;
    }()
    
    fileprivate lazy var newFMlist:[HomeFM] = [HomeFM]()
    
    fileprivate lazy var cachesHeight:[String: CGFloat] = [String:CGFloat]()
}

private let newFMCellIdentifier = "newFMCellIdentifier"

extension NewFMViewController:UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return newFMlist.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: newFMCellIdentifier) as! CategoryTableViewCell
        
        cell.model = newFMlist[indexPath.row]
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        let model = newFMlist[indexPath.row]
        
        let height = cachesHeight[model.id!]
        
        guard height == nil else {
            return height!
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: newFMCellIdentifier) as! CategoryTableViewCell
        
        let rowHeight = cell.rowHeight(model: model)
        
        cachesHeight[model.id!] = rowHeight
        
        return rowHeight
    }
}










